package com.zsr.example.controller.front;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsr.example.entity.EduCourse;
import com.zsr.example.entity.EduTeacher;
import com.zsr.example.service.EduCourseService;
import com.zsr.example.service.EduTeacherService;
import com.zsr.example.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/teacherfront")
@CrossOrigin
public class TeacherController {

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private EduCourseService courseService;

    @GetMapping(value = "{page}/{limit}")
    public JSONObject pageList(
            @PathVariable Long page,
            @PathVariable Long limit){

        Page<EduTeacher> pageParam = new Page<>(page, limit);

        Map<String, Object> map = teacherService.pageListWeb(pageParam);

        return CommonUtil.successJson(map);
    }

    @GetMapping(value = "{id}")
    public JSONObject getById(
            @PathVariable String id){

        //查询讲师信息
        EduTeacher teacher = teacherService.getById(id);

        //根据讲师id查询这个讲师的课程列表
        List<EduCourse> courseList = courseService.selectByTeacherId(id);

        Map<String, Object> result =new HashMap<>();
        result.put("teacher", teacher);
        result.put("courseList", courseList);
        return CommonUtil.successJson(result);
    }
}